import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import {resolve} from 'path'
import process from 'process'
import visualizer from 'rollup-plugin-visualizer'
import vitePluginElementPlus from 'vite-plugin-element-plus'

const application = 'auth'
const event = process.env.npm_lifecycle_event

const plugins = [
  // $ref 为实验性功能,暂不开启
  // vue({
  //   refTransform: true,
  // }),
  vue(),
  vitePluginElementPlus({
    // 如果你需要使用 [component name].scss 源文件，你需要把下面的注释取消掉。
    // 对于所有的 API 你可以参考 https://github.com/element-plus/vite-plugin-element-plus
    // 的文档注释
    useSource: true,
  }),
]

if (event === 'analyzer') {
  plugins.push(
    visualizer({
      open: true,
      gzipSize: true,
      brotliSize: true,
    }),
  )
}

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    sourcemap: true,
  },
  base: event === 'deploy' ? `//cdn.wenzuo.net/ci/${application}/` : '/',
  plugins,
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `
          $--colors: (
            'white': #ffffff,
            'black': #000000,
            'primary': (
              'base': #6366f1,
            ),
            'success': (
              'base': #10b981,
            ),
            'warning': (
              'base': #f59e0b,
            ),
            'danger': (
              'base': #ef4444,
            ),
            'error': (
              'base': #ef4444,
            ),
            'info': (
              'base': #9ca3af,
            ),
          );
        `,
      },
    },
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
    },
  },
  server: {
    port: 8001,
    open: true,
    proxy: {
      '/auth': {
        target: 'https://auth.wenzuo.net',
        // target: 'http://127.0.0.1:9001',
        changeOrigin: true,
      },
    },
  },
})
